//
//  MyResumeVC.m
//  u76ho
//
//  Created by m-air-01 on 15/12/29.
//  Copyright © 2015年 LV. All rights reserved.
//

#import "MyResumeVC.h"
#import "CreateResumeVC.h"
#import "ResumeCollectionViewCell.h"
#import "CreateResumeVC.h"
#import "RusumeHomePageOneModel.h"
#import "ScanResumeViewController.h"
@interface MyResumeVC ()
<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UIAlertViewDelegate>
{
    UICollectionView * _collectionView;
    NSMutableArray * dataSource;
    CGSize size;
}

@property (strong, nonatomic) FeRollingLoader *rollingLoader;
@end

@implementation MyResumeVC

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];

    
//    AppDelegate * app = [UIApplication sharedApplication].delegate;
//    [app.viewController isHiddenCustomTabBarByBoolean:YES];
//    self.navigationController.navigationBarHidden = NO;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self main];
    
    _rollingLoader = [[FeRollingLoader alloc] initWithView:self.view title:@"LOADING"];
    [self.view addSubview:_rollingLoader];
    
    [_rollingLoader show];
    [self addrightItem];
    self.navigationItem.title = @"简历";
//    [self addNavgationBar];
    //[self addBackItem];
    // Do any additional setup after loading the view.
}
-(void)main
{
    dataSource = [[NSMutableArray alloc]init];
    [self.view setBackgroundColor:UIColorFromRGB(0xf1f3f2)];
    size = CGSizeMake(SCREEN_WIDTH-SIZETO6W(15), 142);
    UICollectionViewFlowLayout * flow = [[UICollectionViewFlowLayout alloc]init];
    flow.minimumLineSpacing = SIZETO6H(13);
    flow.scrollDirection = UICollectionViewScrollDirectionVertical;
    flow.sectionInset = UIEdgeInsetsMake(SIZETO6W(15), SIZETO6W(15), SIZETO6W(15), SIZETO6W(15));
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64) collectionViewLayout:flow];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = UIColorFromRGB(0xf1f3f2);
    [_collectionView registerNib:[UINib nibWithNibName:@"ResumeCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"ResumeCollectionViewCell"];
    [self.view addSubview:_collectionView];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self requestData];
}
-(void)requestData
{
    __weak typeof(self)weakSelf = self;
    [U76HONet resumeHomepageWithAction:@"" code:[Singleton share].code andDataBlock:^(NSArray *arr) {
        NSLog(@"简历首页获取数据成功");
        if (arr.count == 0) {
            UIView * view = (UIView *)[weakSelf.view viewWithTag:504];
            if (view) {
                [view removeFromSuperview];
            }
            [weakSelf nosearch];
            [_rollingLoader removeFromSuperview];
        }
        else
        {
            UIView * view = (UIView *)[weakSelf.view viewWithTag:504];
            if (view) {
                [view removeFromSuperview];
            }
            if (dataSource.count!=0) {
                [dataSource removeAllObjects];
            }
            [dataSource addObjectsFromArray:arr];
            [_collectionView reloadData];
            [_rollingLoader removeFromSuperview];
        }
    }];
}
-(void)nosearch
{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
    view.backgroundColor = UIColorFromRGB(0xf1f3f2);
    view.tag = 504;
    
    UIImageView * imgV = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-98/2,SIZETO6H(120), 98, 100)];
    imgV.image = [UIImage imageNamed:@"苦脸"];
    [view addSubview:imgV];
    
    UILabel * tipLab = [[UILabel alloc]initWithFrame:CGRectMake(0, imgV.bottom+SIZETO6H(20), SCREEN_WIDTH, 20)];
    tipLab.text = @"暂时没有符合条件的内容";
    tipLab.textAlignment = NSTextAlignmentCenter;
    tipLab.font = [UIFont systemFontOfSize:14];
    tipLab.textColor = UIColorFromRGB(0x333944);
    [view addSubview:tipLab];
    [self.view addSubview:view];
}

-(void)dealResumeWithtype:(NSString *)type ids:(NSString *)rid
{
    __weak typeof(self)weakSelf = self;
    [U76HONet resumeIsdoWithAction:@"" code:[Singleton share].code type:type rid:rid andDataBlcok:^(NSDictionary *dic) {
        NSLog(@"对简历的操作成功了");
        [weakSelf requestData];
    }];
}
#pragma mark - _collectionViewDelegate
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return dataSource.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ResumeCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ResumeCollectionViewCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
       if (dataSource.count != 0) {
           RusumeHomePageOneModel * cellModel = dataSource[indexPath.row];
           cell.model = cellModel;
           __weak typeof(self)weakSelf = self;
            [cell setDeleteBlock:^{
                NSLog(@"删除简历:%ld",(long)indexPath.row);
                
                if ([weakSelf isLogin]) {
                    [weakSelf dealResumeWithtype:@"del" ids:cellModel.rid];

                }
            }];
            [cell setRefreshBlock:^{
                NSLog(@"刷新啦");
                if ([weakSelf isLogin]) {
                    [weakSelf dealResumeWithtype:@"fresh" ids:cellModel.rid];

                }
            }];
            [cell setBlock:^(BOOL isEditing) {
                if (isEditing) {
                    NSLog(@"编辑了");
                    if ([weakSelf isLogin]) {
                        CreateResumeVC * createResumeVC = [[CreateResumeVC alloc]init];
                        createResumeVC.isEditing = YES;
                        createResumeVC.rid = cellModel.rid;
                        [weakSelf.navigationController pushViewController:createResumeVC animated:YES];
                        [createResumeVC setEditingBlock:^{
                            [weakSelf requestData];
                        }];
                    }
                    
                }
                else
                {
                    NSLog(@"首选地址");
                    [weakSelf dealResumeWithtype:@"istop" ids:cellModel.rid];
                }
            }];
    }
    
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return size;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self isLogin]) {
        RusumeHomePageOneModel * cellModel = dataSource[indexPath.row];
        ScanResumeViewController * scanresumeVC = [[ScanResumeViewController alloc]init];
        scanresumeVC.rid = cellModel.rid;
        scanresumeVC.isDidselected = YES;
        [self.navigationController pushViewController:scanresumeVC animated:YES];
    }
    
}
-(BOOL)isLogin
{
    if ([Singleton share].code) {
        NSLog(@"登录了");
        return YES;
        
    }
    else
    {
        //        NSLog(@"还没登录呢");
        //        LoginViewController * loginVC = [[LoginViewController alloc]init];
        //        [self.navigationController pushViewController:loginVC animated:NO];
        //        return NO;
        if (SYSTEM_VERSION >= 8.0) {
            UIAlertController * alerVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"您还没有登录是否登录" preferredStyle:UIAlertControllerStyleAlert];
            [alerVC addAction:[UIAlertAction actionWithTitle:@"登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
//                app.window.rootViewController = nvc;
                LoginViewController * loginVC = [[LoginViewController alloc]init];
                UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:loginVC];
                nvc.navigationBarHidden = YES;
                
                [self presentViewController:nvc animated:YES completion:nil];
                
            }]];
            [alerVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                
            }]];
            [self.view.window.rootViewController presentViewController:alerVC animated:YES completion:nil];
            
        }
        else
        {
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您还没有登录是否登录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
        }
        return NO;
        
    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    int i = (int)buttonIndex;
    if (i == 0) {
        
    }
    else if(i == 1)
    {

        //app.window.rootViewController = nvc;
    
        LoginViewController * loginVC = [[LoginViewController alloc]init];
        UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:loginVC];
        nvc.navigationBarHidden = YES;
        
        [self presentViewController:nvc animated:YES completion:nil];
    
    }
}
#pragma mark - addRightItem
-(void)addrightItem
{
    UIButton * createBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    createBtn.frame = CGRectMake(0, 0, 60, 30);
    [createBtn setTitle:@"创建" forState:UIControlStateNormal];
    [createBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [createBtn addTarget:self action:@selector(createBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc]initWithCustomView:createBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
}
-(void)createBtnAction:(id)sender
{
    CreateResumeVC * createResumeVC = [[CreateResumeVC alloc]init];
    __weak typeof(self)weakSelf = self;

    [createResumeVC setCreateBlock:^{
        [weakSelf requestData];
    }];
    [self.navigationController pushViewController:createResumeVC animated:YES];
}
-(void)edtiDelegateWithStr:(NSString *)str
{
    NSLog(@"编辑的回调 成功了");

    [self requestData];
}
#pragma  mark - 返回按钮图标
-(void)addBackItem{
    UIButton * backItem = [UIButton buttonWithType:UIButtonTypeCustom];
    backItem.frame = CGRectMake(0, 0, 9, 17);
    [backItem setImage:[UIImage imageNamed:@"返回图标"] forState:UIControlStateNormal];
    [backItem addTarget:self action:@selector(backItem:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backItem];
}
-(void)backItem:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
